package com.heima.search.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.user.entity.ApUser;
import com.heima.search.entity.ApUserSearch;
import com.heima.search.entity.HistorySearchDto;
import com.heima.search.service.ApUserSearchService;
import com.heima.search.utils.AppUserThreadLocalUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl implements ApUserSearchService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public void insert(String keyword, Long userId) {
        if (keyword==null || userId == null) {
            return ;
        }
        //1.判断当前关键词记录是否存在

        Query query = Query.query(Criteria.where("keyword").is(keyword).and("userId").is(userId));
        ApUserSearch apUserSearch = mongoTemplate.findOne(query, ApUserSearch.class);
        //2.存在则更新创建时间
        if (apUserSearch != null) {
           apUserSearch.setCreatedTime(new Date());
           mongoTemplate.save(apUserSearch);
        }
        //3.不存在则插入
        ApUserSearch apUserSearch1 = new ApUserSearch();
        apUserSearch1.setKeyword(keyword);
        apUserSearch1.setUserId(userId.intValue());
        apUserSearch1.setCreatedTime(new Date());

        //3.1  判断当前用户记录条数是否超过10条
        Query query1 = Query.query(Criteria.where("userId").is(userId));
        query1.with(Sort.by("createdTime").descending());
        List<ApUserSearch> apUserSearchList = mongoTemplate.find(query1,ApUserSearch.class);
        if (apUserSearchList.size() >=10) {
            //3.2  超过10条,则替换离现在最远的记录
            ApUserSearch apUserSearch2 = apUserSearchList.get(apUserSearchList.size() - 1);
            mongoTemplate.findAndReplace(Query.query(Criteria.where("id").is(apUserSearch2.getId())),apUserSearch1);
        }
        else {
            //3.3  没超过10条直接插入
            mongoTemplate.save(apUserSearch1);
        }


    }

    @Override
    public ResponseResult findUserSearch() {
        ApUser apUser = AppUserThreadLocalUtil.getUser();
        if (apUser==null) {
            //未登录,没有历史记录
            return ResponseResult.okResult("");
        }

        Query query = Query.query(Criteria.where("userId").is(apUser.getId())).with(Sort.by("createdTime").descending());
        List<ApUserSearch> apUserSearches = mongoTemplate.find(query, ApUserSearch.class);

        return ResponseResult.okResult(apUserSearches);
    }

    @Override
    public ResponseResult delUserSearch(HistorySearchDto historySearchDto) {

        if (historySearchDto.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        ApUser user = AppUserThreadLocalUtil.getUser();
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        Query query = Query.query(Criteria.where("userId").is(user.getId()).and("id").is(historySearchDto.getId()));
        mongoTemplate.findAndRemove(query,ApUserSearch.class);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
